package com.compty.core.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.compty.core.framework.util.StringUtil;

public class CommonsUtil {
	
	private final static Logger logger = Logger.getLogger(CommonsUtil.class);

	/**小写md5加密**/
	public static String getMD5(String content) {
		
		if(content == null) return null;
		
		byte[] source = content.getBytes();
		String s = null;
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(source);

			byte tmp[] = md.digest();
			char str[] = new char[16 * 2];

			int k = 0;
			for (int i = 0; i < 16; i++) { // 

				byte byte0 = tmp[i];

				str[k++] = hexDigits[byte0 >>> 4 & 0xf];

				str[k++] = hexDigits[byte0 & 0xf];
			}
			s = new String(str);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return s;
	}
	
	public static String encode(byte[] fileData) {

		if (fileData == null) {
			return null;
		}

		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(fileData);
	}

	public static byte[] decode(String fileData) {
		if (fileData == null) {
			return null;
		}
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			return decoder.decodeBuffer(fileData);
		} catch (IOException e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	public static String encodeToString(String string) {
		if (string == null) {
			return null;
		} else {
			return encode(string.getBytes());
		}
	}

	public static String decodeToString(String string) {
		if (string == null) {
			return null;
		} else {
			byte[] _byte = decode(string);
			return new String(_byte);
		}
	}

	public static String getPathSymbol() {
		return System.getProperty("file.separator");
	}

	public static byte[] BlobToBytes(Blob blob) {
		
		if(blob == null) return null;
		try {
			return blob.getBytes(0, (int) blob.length());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte[] InputStreamToByte(InputStream is) throws IOException {
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
		int ch;
		while ((ch = is.read()) != -1) {
			bytestream.write(ch);
		}
		byte imgdata[] = bytestream.toByteArray();
		bytestream.close();
		return imgdata;
	} 
	
	public static boolean deleteFile(File f){
		
		boolean flag = true;
		
		if(f == null) return flag;
		
		if(f.exists()){
			f.delete();
		}
		
		return flag;
	}
	
	public static boolean deleteFile(String filePath){
		if(filePath == null) return true;
		
		return deleteFile(new File(filePath));
	}

	/**
	 * 将string型的ip转换为long型
	 */
	public static long stringIp2Long(String ip) {
		String[] ips = ip.split("\\.");
		if (ips.length != 4)
			return 0;
		long ip1 = Long.parseLong(ips[0]);
		long ip2 = Long.parseLong(ips[1]);
		long ip3 = Long.parseLong(ips[2]);
		long ip4 = Long.parseLong(ips[3]);
		return ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
	}

	/**
	 * 读取属性配置文件
	 */
	public static String getPropertyValue(String propertyName, String key) {
		Properties properties = new Properties();
		try {
			properties.load(CommonsUtil.class.getClassLoader().getResourceAsStream(propertyName));
			String value =  properties.getProperty(key);
			if(value != null){
				return value.trim();
			}
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


	public static void main(String[] args) {
				
		System.out.println(StringUtil.getRootPath());
		
	}

}
